<template>
  <div v-if="isAuth(['ROOT', 'USER:SELECT'])">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataForm">
      <el-form-item prop="name">
        <el-input
            v-model="dataForm.name"
            placeholder="姓名"
            size="medium"
            class="input"
            clearable="clearable"
        />
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.sex" class="input" placeholder="性别" size="medium" clearable="clearable">
          <el-option label="男" value="男"/>
          <el-option label="女" value="女"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.role" class="input" placeholder="角色" size="medium" clearable="clearable">
          <el-option v-for="one in roleList" :label="one.roleName" :value="one.roleName"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
            v-model="dataForm.deptId"
            class="input"
            placeholder="部门"
            size="medium"
            clearable="clearable"
        >
          <el-option v-for="one in deptList" :label="one.deptName" :value="one.id"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
            v-model="dataForm.status"
            class="input"
            placeholder="状态"
            size="medium"
            clearable="clearable"
        >
          <el-option label="在职" value="1"/>
          <el-option label="离职" value="2"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button size="medium" type="primary" @click="searchHandle()">查询</el-button>
        <el-button
            size="medium"
            type="primary"
            :disabled="!isAuth(['ROOT', 'USER:INSERT'])"
            @click="addHandle()"
        >
          新增
        </el-button>
        <el-button
            size="medium"
            type="danger"
            :disabled="!isAuth(['ROOT', 'USER:DELETE'])"
            @click="deleteHandle()"
        >
          批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        cell-style="padding: 4px 0"
        style="width: 100%;"
        size="medium"
    >
      <el-table-column type="selection" header-align="center" align="center" width="50"/>
      <el-table-column type="index" header-align="center" align="center" width="50" label="序号">
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="name" header-align="center" align="center" min-width="80" label="姓名"/>
      <el-table-column prop="sex" header-align="center" align="center" min-width="60" label="性别"/>
      <el-table-column prop="tel" header-align="center" align="center" min-width="120" label="电话"/>
      <el-table-column
          prop="email"
          header-align="center"
          align="center"
          min-width="220"
          label="邮箱"
          :show-overflow-tooltip="true"
      />
      <el-table-column prop="hiredate" header-align="center" align="center" min-width="110" label="入职日期"/>
      <el-table-column
          prop="roles"
          header-align="center"
          align="center"
          min-width="150"
          label="角色"
          :show-overflow-tooltip="true"
      />
      <el-table-column prop="deptName" header-align="center" align="center" min-width="70" label="部门"/>
      <el-table-column prop="status" header-align="center" align="center" min-width="50" label="状态"/>
      <el-table-column header-align="center" align="center" width="150" label="操作">
        <template #default="scope">
          <el-button
              type="text"
              size="medium"
              v-if="isAuth(['ROOT', 'USER:UPDATE'])"
              @click="updateHandle(scope.row.id)"
          >
            修改
          </el-button>
          <el-button
              type="text"
              size="medium"
              v-if="isAuth(['ROOT', 'USER:UPDATE'])"
              :disabled="scope.row.status === '离职' || scope.row.root"
              @click="dismissHandle(scope.row.id)"
          >
            离职
          </el-button>
          <el-button
              type="text"
              size="medium"
              :disabled="scope.row.root || scope.row.status === '在职'"
              v-if="isAuth(['ROOT', 'USER:DELETE'])"
              @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalCount"
        layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="loadDataList"></add-or-update>
    <dismiss v-if="dismissVisible" ref="dismiss" @refreshDataList="loadDataList"></dismiss>
  </div>
</template>

<script>
import AddOrUpdate from './components/user-add-or-update.vue';
import Dismiss from './components/dismiss.vue';

export default {
  components: {
    AddOrUpdate,
    Dismiss
  },
  data() {
    return {
      dataForm: {
        name: '',
        sex: '',
        role: '',
        deptId: '',
        status: ''
      },
      dataList: [],
      roleList: [],
      deptList: [],
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      dismissVisible: false,
      dataRule: {
        name: [{required: false, pattern: '^[\u4e00-\u9fa5]{1,10}$', message: '姓名格式错误'}]
      }
    };
  },
  methods: {
    loadDataList() {
      this.dataListLoading = true;
      let data = {
        page: this.pageIndex,
        length: this.pageSize,
        name: this.dataForm.name,
        sex: this.dataForm.sex,
        role: this.dataForm.role,
        deptId: this.dataForm.deptId,
        status: this.dataForm.status
      }
      this.$http("user/searchUserByPage", "POST", data, true, (resp) => {
        if (resp.code === 200) {
          let page = resp.page;
          let list = page.list;
          for (let item of list) {
            if (item.status === 1) {
              item.status = "在职"
            } else if (item.status === 2) {
              item.status = "离职"
            }
          }
          this.dataList = list;
          console.log(list)
          this.totalCount = page.totalCount;
          this.dataListLoading = false;
        }
      })
    },
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.loadDataList();
    },
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.loadDataList();
    },
    searchHandle() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs['dataForm'].clearValidate();
          if (this.dataForm.name === '') {
            this.dataForm.name = null;
          }
          if (this.pageIndex !== 1) {
            this.pageIndex = 1;
          }
          this.loadDataList();
        } else {
          return false;
        }
      })
    },
    loadRoleList: function () {
      let that = this;
      that.$http('role/searchAllRole', 'GET', null, true, function (resp) {
        that.roleList = resp.list;
      });
    },
    loadDeptList: function () {
      let that = this;
      that.$http('dept/searchAllDept', 'GET', null, true, function (resp) {
        that.deptList = resp.list;
      });
    },
    selectionChangeHandle: function (val) {
      this.dataListSelections = val;
    },
    addHandle() {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init();
      })
    },
    updateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      })
    },
    dismissHandle(id) {
      let ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id
          })

      if (ids.length === 0) {
        this.$message({
          message: "您没有选中记录",
          type: "warning",
          duration: 1200
        })
      } else {
        this.$confirm("确定要将选中的进行离职处理吗？", "提示", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.dismissVisible = true;
          this.$http("user/dismissUserByIds", "POST", {ids}, true, (resp) => {
            if (resp.rows > 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1200
              })
              this.loadDataList();
            } else {
              this.$message({
                message: "操作失败",
                type: "warning",
                duration: 1200
              })
            }
          })
        })
      }
    },
    deleteHandle(id) {
      let ids = id
          ? [id]
          : this.dataListSelections.map(item => {
            return item.id
          })

      if (ids.length === 0) {
        this.$message({
          message: "没有选中记录",
          type: "warning",
          duration: 1200
        })
      } else {
        this.$confirm("确定要删除选中的记录吗？", "提示", {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http("user/deleteUserByIds", "POST", {ids}, true, (resp) => {
            if (resp.rows > 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1200
              })
              this.loadDataList();
            } else {
              this.$message({
                message: "删除失败",
                type: "warning",
                duration: 1200
              })
            }
          })
        })
      }
    }

  },
  created: function () {
    this.loadDataList();
    this.loadRoleList();
    this.loadDeptList();
  }
};
</script>

<style lang="less" scoped="scoped"></style>
